package com.marvels.approve.task;

import java.util.UUID;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.marvels.approve.common.utils.ApplicationContextAchieve;
import com.marvels.common.utils.MarvelsLogUtil;


public class QuartzJobLauncher extends QuartzJobBean {

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		//获取设置的任务属性数据
		JobDetail jobDetail = context.getJobDetail();
		JobDataMap jobDataMap = jobDetail.getJobDataMap();
		Job job = (Job)jobDataMap.get("job");
		MarvelsLogUtil.getInstance().info("======>jobName:" + job.getName() + " starting ");
		
		try {
			JobParameters jobParameters = new JobParametersBuilder().addString("uuid", UUID.randomUUID().toString())
					.toJobParameters();
			MarvelsLogUtil.getInstance().info("======>jobName:" + job.getName() + ",jobParameters:" + jobParameters);
			//调度job
			SimpleJobLauncher jobLauncher = (SimpleJobLauncher)ApplicationContextAchieve.getApplicationContext().getBean("jobLauncher");
			jobLauncher.run(job, jobParameters);
		
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>jobName:" + job.getName() + " -跑批任务异常", e);
		}

	}

}